package cn.yanxiaohui.service.web.mq.exchange;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * 简单的消息接收
 * @author yanxh
 */
public class RecvMQ {
    private final static String QUEUE_NAME = "ChongDongOrder";

    public static void main(String[] args) throws IOException, Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("chongdong");
        factory.setPassword("chongdong");
        factory.setVirtualHost("saibeideyu");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                                       byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println("中间件接收消息【" + message + "】");
            }
        };
        // 自动确认
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }


}
